package org.pragma.grammar;

import java.io.File;

import com.sun.xml.internal.bind.v2.runtime.output.IndentingUTF8XmlOutput;

import fri.patterns.interpreter.parsergenerator.Parser;
import fri.patterns.interpreter.parsergenerator.Token;
import fri.patterns.interpreter.parsergenerator.builder.SerializedParser;
import fri.patterns.interpreter.parsergenerator.semantics.ReflectSemantic;

public class PragmaParser extends ReflectSemantic
{
    private static String [][] rules = {
    	
    	{ "START", "EXPRESSIONS" },
    	
    	{ "EXPRESSIONS",   "EXPRESSION", "EXPRESSIONS" },
        { "EXPRESSIONS",   "EXPRESSION" },
        
        { "EXPRESSION",   "VALUABLE"},
        //{ "EXPRESSION",   "STATEMENT" },
        
        { "VALUABLE",   "CALL", },
        { "VALUABLE",   "REFERENCE", },
        
        { "CALL",  "REFERENCE" , "'.'", "REFERENCE" , "'('" , "PARAMETERS", "')'" },
       
        { "PARAMETERS",   "PARAMETER", "PARAMETERS" },
        { "PARAMETERS",   "PARAMETER" },
        
        
        { "PARAMETER",   "VALUABLE", },
        { "PARAMETER",   "BLOCK", },
        
        
        { "BLOCK",   "'{'","EXPRESSIONS", "'}'" },
        /*{ "STATEMENT",   "IF", },
        { "STATEMENT",   "WHILE", },
        { "STATEMENT",   "STORE", },

        { "IF", "'if'",  "VALUABLE", "BLOCK" },
        { "IF", "'if'",  "VALUABLE", "BLOCK",  "'then'", "BLOCK", },
        
        { "WHILE",   "'while'", "VAUABLE", "BLOCK" },
        
        { "STORE",   "REFERENCE", "'='", "PARAMETER"},
        */
        
        { "REFERENCE",   "VARIABLE", },
        { "VARIABLE",   "`identifier`", },

        { Token.IGNORED,   "`whitespaces`" }
    };
    
    public Object START(Object EXPRESSION)    {
    	System.out.println("START "+EXPRESSION);
        return EXPRESSION;
    }
    public Object EXPRESSIONS(Object EXPRESSION)    {
        return EXPRESSION;
    }
    public Object EXPRESSIONS(Object EXPRESSION,  Object EXPRESSIONS)    {
        return EXPRESSION+" "+EXPRESSIONS;
    }
    public Object EXPRESSION(Object VALUABLE_STATEMENT)    {
        return VALUABLE_STATEMENT;
    }
  
    public Object VALUABLE(Object CALL_REFERENCE)    {
        return CALL_REFERENCE;
    }
    
    public Object CALL(Object REFERENCE,Object dot,Object REFERENCE2, Object par, Object PARAMETERS, Object par2)    {
    	System.out.println("CALL:"+REFERENCE+"."+REFERENCE2+"("+PARAMETERS+")");
        return REFERENCE+" "+REFERENCE2+" "+par+" "+PARAMETERS+" "+par2;
    }

    public Object PARAMETERS(Object PARAMETER)    {
        return PARAMETER;
    }
    public Object PARAMETERS(Object PARAMETER,  Object PARAMETERS)    {
        return PARAMETERS;
    }
    public Object PARAMETER(Object VALUABLE_BLOCK)    {
        return VALUABLE_BLOCK;
    }
    public Object BLOCK( Object open, Object EXPRESSIONS, Object close)    {
        return open+" "+EXPRESSIONS+" "+close;
    }
    public Object REFERENCE(Object VARIABLE)    {
        return VARIABLE;
    }
    public Object VARIABLE(Object IDENTIFIER)    {
    	System.out.println("IDENTIFIER "+ IDENTIFIER);
        return IDENTIFIER;
    }
    /*public Object STATEMENT(Object IF_WHILE_STORE)    {
        return null;
    }*/
    
    
    
    
    public static void main(String [] args) throws Exception   {
    	File f = new File("/home/flo/.friware/parsers/PragmaParser.ser");
    	f.delete();
        //String input = args[0];   // define some arithmetic expressions on commandline
    	String input = "home .go(par)";
        Parser parser = new SerializedParser().get(rules, "Pragma");    // allocates a default lexer
        boolean ok = parser.parse(input, new PragmaParser());
        System.err.println("Parse return "+ok+", result: "+parser.getResult());
    }
}